<template>
    <div class="view-body">
        <!--筛选-->
        <div class="header-select">
            <van-dropdown-menu active-color="#075ebb">
                <!-- 问题分类 -->
                <van-dropdown-item :title="primaryTitle" @open="selectedClick1()" ref="queRef">
                    <template #default>
                        <div class="firstCata" v-show="primaryClassification">
                            <van-cell :title="$t('type_all_title')" :class="{'cur': selectClassificationOne === '0'}" @click="knowSelectClassifyAll()">
                                <template #right-icon >
                                        <van-icon name="success" class="cur" v-if="selectClassificationOne === '0'" />
                                </template>
                            </van-cell>
                            <van-cell-group class="firstCata" v-for="classification in knowClassifications" :key="classification.kcaId" @click="knowSelectClassificationOne(classification.kcaId, classification.childKnowCatalogList.length, classification.kcaTitle)">
                                <van-cell :title="classification.kcaTitle" border :class="{'cur': selectClassificationOne === classification.kcaId}" >
                                    <template #right-icon >
                                        <van-icon name="arrow" v-if="classification.childKnowCatalogList.length > 0"/>
                                        <van-icon name="success" class="cur" v-if="classification.childKnowCatalogList.length <= 0 && selectClassificationOne === classification.kcaId" />
                                    </template>
                                </van-cell>
                            </van-cell-group>
                        </div>
                        <div class="secondCata" v-show="secondaryClassification" >
                            <van-cell :title="$t('know_back_to_previous')" @click="backToPrevious()"/>
                            <van-cell-group class="firstCata"  v-for="classification in knowSecondList" :key="classification.kcaId" @click="knowSelectClassificationTwo(classification.kcaId, classification.kcaTitle)">
                                <van-cell :title="classification.kcaTitle" border :class="{'cur': selectClassificationTwo === classification.kcaId}">
                                    <template #right-icon >
                                        <van-icon name="success" class="cur" v-if="selectClassificationTwo === classification.kcaId" />
                                    </template>
                                </van-cell>
                            </van-cell-group>
                        </div>
                    </template>
                </van-dropdown-item>
            </van-dropdown-menu>
        </div>
        <div style="height: calc(100% - 50px);" class="body-content" :class="{'height-100' : total === 0}">
            <van-pull-refresh v-model="isRefresh" @refresh="onPulldownTop" loading-text="loading" pulling-text="" loosing-text="" style="height: 100%;">
                <van-list
                v-model="isLoading"
                :finished="finished"
                :finished-text="(finished&&total>0) ? $t('no_more') : ''"
                loding-text="loading"
                @load="getMyAnswerList"
                >
                    <div v-bind:class="{'height100' : total === 0}">
                        <ul class="white-bg module-list-09">
                            <li v-for="myAnswer in myAnswerList" :key="myAnswer.id" @click="detatilJump(myAnswer.queType, myAnswer.id)">
                                <p class="font-size17">{{ myAnswer.title }}</p>
                                <div class="color999" style="margin-top: 8px;">
                                    <span>{{ myAnswer.createTime.split(' ')[0] }}</span><em class="point"></em><span>{{ $t("know_from") }}{{ myAnswer.usrDisplayBil }}</span><em class="point" v-if="myAnswer.queBounty > 0"></em><span v-if="myAnswer.queBounty > 0">{{ $t("know_bounty") }}{{ myAnswer.queBounty }}</span><!--来自/悬赏积分-->
                                </div>
                                <div class="font-size14 color666 word-clamp-3" style="margin-top: 15px;" v-if="myAnswer.ansUser">{{ myAnswer.ansUser.usrDisplayBil }}：{{ myAnswer.knowAnswer.ansContent }}</div>
                                <div class="module-box-16 clearfix">
                                    <span class="color-box" v-bind:class="{'bgcolor-red': myAnswer.queType === 'UNSOLVED', 'bgcolor-green': myAnswer.queType === 'SOLVED'}">{{ knowListStatus[myAnswer.queType] }}</span><span class="color-box" v-if="myAnswer.queType === 'SOLVED'" style="margin-left: 5px;">{{ $t("know_popular") }}</span><span v-if="myAnswer.knowCatalog"><i class="tag-icon"></i><span class="font-size11 color999">{{ myAnswer.knowCatalog.title }}</span></span><span class="font-size11 color999" style="margin-left: 10px;">{{ myAnswer.askNum ? myAnswer.askNum : 0 }}{{ myAnswer.askNum > 1 ? $t("know_reply_num_2") : $t("know_reply_num") }}</span><!--精选/条回复-->
                                </div>
                            </li>
                        </ul>
                        <div class="bottom-shadow" style="height: 1px;margin-top: -1px;" v-if="!isLoading && total > 0"></div>
                        <!--暂无数据-->
                        <div v-if="!isLoading && total === 0" style="height:100%;">
                            <div class="box-table" style="width:100%;height:100%;">
                                <div class="box-table-cell" style="text-align: center;">
                                    <div class="no-data">
                                        <img src="../../../static/images/no-data.png" alt=""/>
                                        <p class="color999">{{ $t('no_data') }}</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </van-list>
            </van-pull-refresh>
        </div>
        <common-loading :isLoading="isLoading"></common-loading>
    </div>
</template>

<script>
export default {
    name: 'MyAnswer',
    data() {
        return {
            pageNo: 1, //分页页码
            total: 0,
            myAnswerList: [], //问题列表
            knowClassifications: [], //问题分类
            knowSecondList: [], //二级分类
            knowClassificationType: 'CATALOG', //问题分类默认一级分类
            knowClassification: '0', //问题分类默认
            primaryClassification: false, //一级分类
            secondaryClassification: false, //二级分类
            selectClassificationOne: '', //一级问题分类
            selectClassificationTwo: '', //二级问题分类
            selectClassificationOneTitleId: '', //二级分类的一级id
            selectClassificationTwoOne: false, //二级问题分类的一级分类
            keepClassificationOneId: 0, //保存一级分类的id
            knowStatusNum: {}, //问题状态的总数
            knowType: 'ONANSWER', //问题状态默认
            selectStatus: null, //问题状态
            isShow1: false,
            isShow2: false,
            unSolved: 0, //问题状态-->待解决（数量）
            solved: 0, //问题状态-->已解决（数量）
            getStatus: [
                {type: 'all', value: this.$t('type_all_title')}, //全部
                {type: 'UNSOLVED', value: this.$t('know_wait')}, //待解决
                {type: 'SOLVED', value: this.$t('know_anwsered')}, //已解决
                {type: 'FAQ', value: this.$t('know_faq')} //FAQ
            ],
            knowListStatus: {
                UNSOLVED: this.$t('know_wait'), //待解决
                SOLVED: this.$t('know_anwsered'), //已解决
                FAQ: this.$t('know_faq') //FAQ
            },
            isLoading: true,
            finished: false,
            isRefresh: false,
            primaryTitle: this.$t('know_ask_que_classify')
        };
    },
    methods: {
        selectedClick1() {
            this.primaryClassification = true;
            this.secondaryClassification = false;
        },
        onPulldownTop() {
            this.isRefresh = true;
            setTimeout(() => {
                this.finished = false;
                this.pageNo = 1;
                this.getMyAnswerList('refresh');
                this.isRefresh = false;
            }, 900);
        },
        getMyAnswerList(option) { //列表请求方法
            this.$axios({
                method: 'POST',
                // url: '/app/know/myKnow/my_answer',
                url: `/app/api/trainee/know/list/page`,
                params: {
                    pageNo: this.pageNo
                    // queType: this.knowType,
                    // kcaId: this.knowClassification
                },
                data: {
                    queType: this.knowType.toUpperCase(),
                    catalogId: parseInt(this.knowClassification),
                    ancestorId: parseInt(this.keepClassificationOneId)
                }
            }).then((res) => {
                this.isTop = true;
                this.onFetching = false;
                if (res.data.results && res.data.results.length !== 0) {
                    if (option && option === 'refresh') {
                        this.myAnswerList = res.data.results;
                    } else {
                        this.myAnswerList = [...this.myAnswerList, ...res.data.results];
                    }
                }
                this.total = res.data.totalRecord;
                this.isLoading = false;
                this.pageNo++;
                if (this.myAnswerList.length >= this.total) {
                    this.finished = true;
                }
            });
        },
        knowSelectStatus(knowStatusType) {
            this.isLoading = true;
            this.myAnswerList = [];
            this.knowType = knowStatusType;
            this.pageNo = 1;
            this.isShow2 = false;
            this.selectStatus = knowStatusType;
            this.getMyAnswerList();
        },
        //一级分类
        getClassificationList() {
            this.$axios({
                method: 'GET',
                // url: '/app/know/allKnow/kca/' + this.knowClassificationType + '/' + this.knowClassification
                url: `/app/api/trainee/know/kca/` + this.knowClassificationType + '/' + parseInt(this.knowClassification)
            }).then((res) => {
                this.knowClassifications = res.data;
            });
        },
        //分类“全部”按钮
        knowSelectClassifyAll() {
            this.isLoading = true;
            this.isShow1 = false;
            this.myAnswerList = [];
            this.pageNo = 1;
            this.knowClassificationType = 'CATALOG';
            this.knowClassification = '0';
            this.selectClassificationOne = '0';
            this.selectClassificationTwo = '';
            this.keepClassificationOneId = '0';
            this.primaryTitle = this.$t('type_all_title');
            this.getMyAnswerList();
            this.$refs.queRef.toggle(false);
        },
        /*一级点击分类*/
        knowSelectClassificationOne(ClassificationId, listTotal, title) {
            this.isLoading = true;
            this.knowClassification = ClassificationId;
            if (listTotal > 0) {
                this.$refs.queRef.toggle(true);
                this.primaryClassification = false;
                this.secondaryClassification = true;
                this.primaryTitle = title;
                this.getsecondList(ClassificationId);
            } else {
                if (this.selectClassificationTwo !== '') {
                    this.selectClassificationTwo = '';
                }
                this.selectClassificationOne = ClassificationId;
                this.selectClassificationTwoOne = true; //在二级分类点击的一级分类
                this.selectClassificationOneTitleId = ClassificationId; //在二级分类页点击一级分类，保存其id
                this.myAnswerList = [];
                this.primaryTitle = title;
                this.pageNo = 1;
                this.isShow1 = false;
                this.getMyAnswerList();
                this.$refs.queRef.toggle(false);
            }
        },
        /*二级点击分类*/
        knowSelectClassificationTwo(ClassificationId, title) {
            this.isLoading = true;
            this.knowClassification = ClassificationId;
            this.selectClassificationTwo = ClassificationId;
            this.selectClassificationOne = this.keepClassificationOneId; //保存一级分类的id，在一级分类页为蓝色字体
            this.myAnswerList = [];
            this.primaryTitle = title;
            this.pageNo = 1;
            this.isShow1 = false;
            this.getMyAnswerList();
            this.$refs.queRef.toggle(false);
        },
        /*二级分类显示列表*/
        getsecondList(Id) {
            this.$axios({
                method: 'GET',
                // url: `/app/know/allKnow/kca/NORMAL/${Id}`
                url: `/app/api/trainee/know/kca/NORMAL/${Id}`
            }).then((res) => {
                this.knowSecondList = res.data;
                this.keepClassificationOneId = Id; //保存一级分类的id
                this.isLoading = false;
            });
        },
        /*返回上一级*/
        backToPrevious() {
            this.secondaryClassification = false;
            this.knowClassifications = [];
            this.primaryClassification = true;
            this.knowClassificationType = 'CATALOG';
            this.knowClassification = '0';
            this.pageNo = 1;
            this.getClassificationList();
        },
        detatilJump(type, id) {
            // let knowQueId = this.wbEncryptor(id);
            this.$router.push({path: `/knowDetail/${type}/${id}`});
        },
        getMyAnswerUnSolved() { //获取我的回答中‘问题分类’中‘待解决’的数量
            this.$axios({
                method: 'POST',
                url: '/app/know/myKnow/my_answer',
                params: {
                    pageNo: this.pageNo,
                    queType: 'UNSOLVED',
                    kcaId: this.knowClassification
                }
            }).then((res) => {
                this.unSolved = res.data.total;
            });
        },
        getMyAnswerSolved() { //获取我的回答‘问题分类’中‘已解决’的数量
            this.$axios({
                method: 'POST',
                url: '/app/know/myKnow/my_answer',
                params: {
                    pageNo: this.pageNo,
                    queType: 'SOLVED',
                    kcaId: this.knowClassification
                }
            }).then((res) => {
                this.solved = res.data.total;
            });
        }
    },
    mounted() {
        this.getMyAnswerList();
        this.getClassificationList(); //一级分类点击
        let $knowList = document.getElementById('knowList');
        if ($knowList) {
            $knowList.classList.remove('weui-bar__item_on');
        }
        let $myQuestion = document.getElementById('myQuestion');
        if ($myQuestion) {
            $myQuestion.classList.add('weui-bar__item_on');
        }
        let $myHelpQuestion = document.getElementById('myHelpQuestion');
        if ($myHelpQuestion) {
            $myHelpQuestion.classList.remove('weui-bar__item_on');
        }
        // this.getMyAnswerUnSolved();
        // this.getMyAnswerSolved();
        //问题状态的总数
        // this.$axios({
        //     method: 'POST',
        //     url: '/app/know/main/json',
        //     params: {
        //         command: 'my_answer'
        //     }
        // }).then((res) => {
        //     this.knowStatusNum = res.data;
        // });
    }
    // beforeRouteEnter(to, from, next) {
    //     next(_this => {
    //
    //     });
    // }
};
</script>

<style scoped>
    .cur {
        color: #075ebb;
    }
</style>
